import 'package:base_flutter/base_flutter.dart';
import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:ls_work/net/api/http_api.dart';
import 'package:ls_work/net/response/patrol/patrol_record_detail_data.dart';
import 'package:ls_work/res/colors.dart';
import 'package:ls_work/util/ext_utils.dart';
import 'package:ls_work/util/request_url_utils.dart';
import 'package:ls_work/viewmodel/patrol/patrol_detail_view_model.dart';
import 'package:ls_work/widgets/add_image_widget.dart';

import '../../widgets/title_value_widget.dart';

class PatrolDetailPage extends BaseStatefulMvvmWidget {
  final String? code;
  final int? id;
  final PatrolRecordDetailData? item;
  final bool isResign;

  PatrolDetailPage(this.code, {this.id, this.item,this.isResign = false});

  @override
  State<StatefulWidget> createState() => _PatrolDetailState();
}

class _PatrolDetailState
    extends BaseMvvmState<PatrolDetailViewModel, PatrolDetailPage> {
  @override
  Widget buildLoadingContentView() {
    return viewModel.detailData == null
        ? Container()
        : viewModel.detailData?.status == 0||widget.item!=null
            ? handleWidget()
            : patrolDetailWidget();
  }

  Widget handleWidget() {
    var data = viewModel.detailData;
    return ListView(
      padding: EdgeInsets.symmetric(horizontal: 16),
      children: [
        Container(height: 10,),
        TitleValueWidget(
          "巡防区域：",
          text: data?.patrolRange ?? "",
          padding: EdgeInsets.zero,
          showArrow: false,
          hint: "",
          textAlign: TextAlign.end,
        ),
        Container(height: 10,),
        TitleValueWidget(
          "巡防任务名称：",
          text: data?.patrolName ?? "",
          padding: EdgeInsets.zero,
          showArrow: false,
          hint: "",
          textAlign: TextAlign.end,
        ),
        Container(height: 10,),
        TitleValueWidget(
          "巡防任务时间：",
          text: "${data?.startTime?.formatDateStr(format: ['HH',':','nn'])}-${data?.endTime?.formatDateStr(format: ['HH',':','nn'])}",
          padding: EdgeInsets.zero,
          showArrow: false,
          hint: "",
          textAlign: TextAlign.end,
        ),
        Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            CommonText(viewModel.detailData?.pointName,
                textSize: 17,
                textColor: Colors.black,
                fontWeight: FontWeight.w600),
            CommonText(viewModel.detailData?.statusName,
                textSize: 15, textColor: Colours.app_main)
          ],
        ).addToContainer(margin: EdgeInsets.symmetric(vertical: 10)),
        Container(
          height: 10,
        ),
        Row(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            CommonText('巡防要求：'),
            CommonText(viewModel.detailData?.require ?? "").setWeight(1)
          ],
        ),
        Container(
          height: 15,
        ),
        viewModel.selectHandleStatusWidget(),
        Container(
          height: 15,
        ),

        if(RequestUrlUtils.code=="zhylycs")
        AddAndUploadImageWidget((files, modelList) {
          setState(() {
            viewModel.fileStringLit = files;
          });

        },files: viewModel.fileStringLit,
          cameramsg: "拍摄巡防处理照片，需要申请您的相机权限",
          photomsg: "从相册选取巡防处理照片，需要申请您的相册权限",),
        CommonText('备注信息'),
        Container(
          height: 10,
        ),
        CommonInput(
          hintText: '请输入备注信息',
          isDense: true,
          padding: EdgeInsets.zero,
          keyboardType: CommonInputType.multiline,
          border: InputBorder.none,
          onChanged: (str) {
            viewModel.feedback = str;
          },
          maxLines: 7,
        ).addToContainer(
            decoration: BoxDecoration(
              borderRadius: 8.borderRadius,
              color: Colors.grey[200],
            ),
            padding: EdgeInsets.all(10)),
        ElevatedButton(
                style: ButtonStyle(
                    backgroundColor:
                        WidgetStateProperty.all(Colours.app_main),
                    minimumSize:
                        WidgetStateProperty.all(Size(double.infinity, 49))),
                onPressed: () {
                  if(widget.item==null){
                    viewModel.submitPatrol();
                  }else{
                    if(widget.isResign){
                      viewModel.submitPatrol(isResign: true);
                    }else{
                      viewModel.uploadAbnormal();
                    }
                  }
                },
                child: CommonText( widget.item==null?'提交并签到': widget.isResign?"补卡提交":"异常登记", textColor: Colors.white))
            .addToContainer(
                margin: EdgeInsets.symmetric(horizontal: 16, vertical: 15))
      ],
    );
  }

  Widget patrolDetailWidget() {
    return ListView(
      padding: EdgeInsets.symmetric(horizontal: 16),
      children: [
        Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            CommonText(viewModel.detailData?.pointName,
                textSize: 17,
                textColor: Colors.black,
                fontWeight: FontWeight.w600),
            CommonText(viewModel.detailData?.statusName,
                textSize: 15, textColor: Colors.green)
          ],
        ).addToContainer(margin: EdgeInsets.symmetric(vertical: 10)),
        Container(
          height: 10,
        ),
        Row(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            CommonText('巡防要求：', height: 1.2),
            CommonText(viewModel.detailData?.require ?? "", height: 1.2)
                .setWeight(1)
          ],
        ),
        Container(
          height: 10,
        ),
        Row(
          crossAxisAlignment: CrossAxisAlignment.start,
          mainAxisAlignment: MainAxisAlignment.start,
          children: [
            CommonText('打卡时间：', height: 1.2),
            CommonText(viewModel.detailData?.executeDate ?? "", height: 1.2)
                .setWeight(1)
          ],
        ),
        Container(
          height: 10,
        ),
        Row(
          crossAxisAlignment: CrossAxisAlignment.start,
          mainAxisAlignment: MainAxisAlignment.start,
          children: [
            CommonText('巡防状态：', height: 1.2),
            CommonText(viewModel.detailData?.resultStatusName, height: 1.2)
                .setWeight(1)
          ],
        ),
        Container(
          height: 10,
        ),
        Row(
          crossAxisAlignment: CrossAxisAlignment.start,
          mainAxisAlignment: MainAxisAlignment.start,
          children: [
            CommonText('打卡人：', height: 1.2),
            CommonText(viewModel.detailData?.executeByName, height: 1.2)
                .setWeight(1)
          ],
        ),
        Container(
          height: 10,
        ),
        Row(
          crossAxisAlignment: CrossAxisAlignment.start,
          mainAxisAlignment: MainAxisAlignment.start,
          children: [
            CommonText('备注：', height: 1.2),
            CommonText(viewModel.detailData?.feedback ?? "", height: 1.2)
                .setWeight(1)
          ],
        ),
        if(viewModel.fileStringLit.isNotEmpty)
         ...[
           Container(height: 16,),
           GridView.count(
             crossAxisCount: 3,
             crossAxisSpacing: 20,
             mainAxisSpacing: 20,
             shrinkWrap: true,
             physics: NeverScrollableScrollPhysics(),
             children: viewModel.fileStringLit
                 .map((path) => ImageLoad(
               "${HttpApi.imageHead}$path",
               fit: BoxFit.cover,
             ).toRound(borderRadius: 4.borderRadius).onTap(() {
               Go().push(ShowImagePage(
                 viewModel.fileStringLit.map((e) => "${HttpApi.imageHead}$path").toList(),
                 image: path,
               ));
             }))
                 .toList(),
           )
         ]
      ],
    );
  }

  @override
  Widget buildRootView(BuildContext context, Widget loadingContentWidget) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.item==null?'巡防点详情':widget.isResign?"补卡":'异常登记'),
        actions: [
          viewModel?.detailList!=null&&viewModel.detailList.length>0?
              IconButton(onPressed: (){
                viewModel.changeDetail();
              }, icon: Icon(Icons.switch_left)):
              Container()
        ],
      ),
      body: loadingContentWidget,
    );
  }

  @override
  PatrolDetailViewModel createViewModel() {
    return PatrolDetailViewModel(context, code: widget.code??"", id: widget.id,item: widget.item);
  }

  @override
  void onRetryClick() {}
}
